package com.chixing.controller;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单消息队列配置类
 */
@Configuration
public class OrderConfigmq {
    //延时交换机
    @Bean
    public DirectExchange orderDelayExchange(){
        return new DirectExchange("orderDelayExchange",true,false);
    }
    //延时队列
    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-message-ttl",50000);
        map.put("x-dead-letter-exchange","orderDeadExchange");
        map.put("x-dead-letter-routing-key","orderDeadKey");
        return new Queue("orderDelayQueue",true,false,false,map);
    }


    //绑定延时队列与延时交换机
    @Bean
    public Binding bindingDelay(){
        return BindingBuilder.bind(orderDelayQueue()).to(orderDelayExchange()).with("orderDelayKey");
    }

    //死信交换机
    @Bean
    public DirectExchange orderDeadExchange(){
        return new DirectExchange("orderDeadExchange",true,false);
    }

    //死信队列
    @Bean
    public Queue orderDeadQueue(){
        return new Queue("orderDeadQueue",true,false,false);
    }

    //绑定
    @Bean
    public Binding bindingDead(){
        return BindingBuilder.bind(orderDeadQueue()).to(orderDeadExchange()).with("orderDeadKey");
    }
}
